<?php

use think\migration\Migrator;

class UserVipLog extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    protected string $name = 'user_vip_log';

    public function up(): void
    {
        /*
        CREATE TABLE `test_user_vip_log` (
          `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
          `module` varchar(30) NOT NULL DEFAULT '' COMMENT '涉及模块',
          `type` varchar(30) NOT NULL DEFAULT 'order' COMMENT '更新类型',
          `order_id` int(11) DEFAULT NULL COMMENT '订单ID',
          `user_id` int(11) NOT NULL COMMENT '用户ID',
          `begin_date` date NOT NULL COMMENT '开始日期',
          `end_date` date NOT NULL COMMENT '结束日期',
          `create_at` datetime NOT NULL COMMENT '记录时间',
          PRIMARY KEY (`id`),
          KEY `order_id` (`order_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
         */
        $table = $this->table($this->name, [
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id'],
            'comment'     => '用户VIP变动记录'
        ]);
        $table->addColumn('user_id', 'integer', [
            'limit' => 11, 'signed'  => false, 'null' => false, 'comment' => '用户ID',
        ])->addColumn('order_id', 'integer', [
            'limit' => 11, 'signed'  => false, 'null' => true, 'comment' => '订单ID',
        ])->addColumn('begin_date', 'date', [
            'null' => false, 'comment' => '开始日期',
        ])->addColumn('end_date', 'date', [
            'null' => false, 'comment' => '结束日期',
        ])->addColumn('remark', 'string', [
            'limit' => 200, 'default' => '', 'null' => false, 'comment' => '备注',
        ])->addColumn('create_at', 'datetime', [
            'null' => false, 'comment' => '记录时间',
        ])->addIndex(['order_id'], [
            'name' => 'order_id'
        ])->addIndex(['user_id'], [
            'name' => 'user_id'
        ])->save();
    }

    public function down(): void
    {
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
